<?php


namespace app\api\model;

use app\api\controller\Code;
use think\Db;
use think\Model;

/**
 * 云信IM
 * Class YunXinModel
 * @package app\api\model
 *
 */
class YunXinModel extends Model
{

//    /**
//     * 设置表名
//     * @var string
//     */
//    protected $table = 'ah_user';
const APPKEY = '8ef235b56fe98e69c71246e1e2f8979e';
const APPSECRET = 'cf0c172ed17d';

    /**
     *
     * 注册云信IM账号
     */
    public static function yunXinAdd($params){

        $userInfo = UserModel::where(['id'=>$params['id']])->find();
        $yunXin = [

            'userUuid'=>$params['id'],
            'name'=>isset($params['nickname']) ? $params['nickname'] :  $userInfo['nickname'] != '' ? $userInfo['nickname'] : 'AI问诊'.$params['id'] ,
            'avatar'=>isset($params['head_img']) ? $params['head_img'] :  $userInfo['nickname'] != '' ? $userInfo['nickname'] : 'https://aiwenzhen.oss-cn-chengdu.aliyuncs.com/20240701/IMAGES/2024-07-01/1719827478115.png' ,
        ];
        $headers = self::headers();
        $res =  postJsonCurl($yunXin,'https://meeting.yunxinroom.com/scene/meeting/api/v2/add-user',1,$headers);
        $res = json_decode($res,true);
        if ($res['code'] == 3406){

            $result =  postJsonCurl($yunXin,'https://meeting.yunxinroom.com/scene/meeting/api/v2/update-user',1,$headers);
            $result = json_decode($result,true);
            if ($result['code'] == 0){

                return true;
            }else{

                return false;
            }
        }elseif ($res['code'] == 0){

            return true;
        }else{

            return false;
        }

    }

    /**
     *
     * 刷新云信IM的token
     */
    public static function refreshToken($id){

        $headers = self::headers();
        $result = postJsonCurl([],"https://meeting.yunxinroom.com/scene/meeting/api/v2/refresh-user-token?userUuid=".$id,1,$headers);
        $result = json_decode($result,true);
        if ($result['code'] == 0){

            return $result['data']['userToken'];
        }else{

            return false;
        }
    }

    /**
     *
     * 创建会议
     */
    public static function createMeeting($id){

        $data = [

            'owner'=>$id,
            'subject'=>'随机会议',
            'startTime'=>(time() * 1000)+ (5 * 60 * 1000),
            'endTime'=>(time() * 1000) + (35 * 60 * 1000),
            'roomConfigId'=>40,
            'roomConfig'=>[

                'resource'=>[

                    'whiteboard'=>true,
                    'record'=>true,
                ]
            ]
        ];
        $AppKey = self::APPKEY;
        $headers = self::headers();
        $res =  postJsonCurl($data,"https://roomkit.netease.im/scene/meeting/api/{$AppKey}/v1/create/3",1,$headers,'PUT');
        return json_decode($res,true);

    }

    /**
     *
     * 结束会议
     */
    public static function meetingDel($meetingId,$recycle = true){
        Db::startTrans();
        $AppKey = self::APPKEY;
        $headers = self::headers();
        $res = postJsonCurl([],"https://roomkit.netease.im/scene/meeting/api/{$AppKey}/v1/close/meetingId/{$meetingId}/recycle/{$recycle}",1,$headers,'DELETE');
        $res = json_decode($res,true);
        $res1 = MeetingModel::where(['meeting_id'=>$meetingId])->update(['is_end'=>1,'update_at'=>time()]);
        $res1 = MeetingModel::meetingEdit(['meeting_id'=>$meetingId],['is_end'=>1,'update_at'=>time()]);//where(['meeting_id'=>$meetingId])->update(['is_end'=>1,'update_at'=>time()]);
        if ($res['code'] == 0 && $res1){

            Db::commit();
            return true;
        }else{

            Db::rollback();
            return false;
        }
    }

    /**
     *
     * 网易云信IM的Headers
     */
    public static function headers(){

        $AppKey = self::APPKEY;
        $AppSecret = self::APPSECRET;
        $nonce_str = nonce_str();
        $CurTime = time();
        $CheckSum = SHA1($AppSecret.$nonce_str.$CurTime);
        return array("AppKey: {$AppKey}","Nonce: {$nonce_str}","CheckSum: {$CheckSum}","CurTime: {$CurTime}","Content-Type: application/json");
    }






}